/**
 * @Description: 该文件提供 周总结 服务
 * @author Fengli
 * @date 5/7/19
*/
import {taskDb} from '@/util/dao/ConfigDB'
import {WeekSummary} from '@/util/entity/WeekSummary'
import {getUUID, getWeekNumber} from '@/util/ApplicationUtil'
// 增
/**
 * 添加周总结
 * @param day 这天必须在对应周的区间里面
 * @returns {*}
 */
export function addWeekSummary (day, content) {
  let ws = new WeekSummary()
  ws.id = getUUID()
  ws.n = getWeekNumber(day)
  ws.year = day.getFullYear()
  ws.content = content
  // 这里的userID在登陆那里赋值
  ws.userId = localStorage.getItem('userId')
  return taskDb.weekSummaryStore.add(ws)
}

/**
 * 批量添加周总结到indexedDB中去
 * @param weekSummarys
 * @returns {Dexie.Promise<Key>}
 */
export function bulkAddWeekSummary (weekSummarys) {
  return taskDb.weekSummaryStore.bulkAdd(weekSummarys)
}

// todo 删

// todo 改
export function updateWeekSummary (ws) {
  return taskDb.weekSummaryStore.update(ws.id, {content: ws.content})
}
// todo 查
/**
 * 查询某一天对应周的周总结
 * @param day
 * @returns {Dexie.Promise<T | undefined>}
 */
export function queryWeekSummary (day) {
  let tn = getWeekNumber(day)
  return taskDb.weekSummaryStore.where({year: day.getFullYear(), n: tn}).first()
}
